package com.example.meterialproject.activitys

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.core.view.contains
import androidx.core.view.forEach
import com.example.meterialproject.base.BaseActivity
import com.example.meterialproject.R
import com.example.meterialproject.databinding.ActivityMaterialChipBinding
import com.example.meterialproject.exts.TAG
import com.example.meterialproject.exts.click
import com.example.meterialproject.exts.takeView
import com.example.meterialproject.exts.toast
import com.google.android.material.chip.Chip


/**
 *
 * @ClassName: ChipActivity
 * @Author: android 超级兵
 * @CreateDate: 1/8/22$ 10:22 AM$
 * TODO chip页面
 * 参考文档:https://material.io/components/chips/android#choice-chip
 */
class ChipActivity : BaseActivity<ActivityMaterialChipBinding>() {

    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding.chip6.setOnCheckedChangeListener { _, isChecked ->
            binding.chip6.isChipIconVisible = !isChecked

            binding.chip6.isChipIconVisible toast this
        }

        /*
         * 作者:android 超级兵
         * 创建时间: 1/8/22 2:46 PM
         * TODO
         *  style="@style/Widget.MaterialComponents.Chip.Action" 默认
         *  style="@style/Widget.MaterialComponents.Chip.Filter" ChipGroup 中 chip 左侧对号
         *  style="@style/Widget.MaterialComponents.Chip.Entry"  Chip 右侧错号
         *  Widget.MaterialComponents.Chip.Choice  Chip 留色
         */

        /*
         * 作者:android 超级兵
         * 创建时间: 1/8/22 3:00 PM
         * TODO 只有设置一行属性时才能使用该方法「不生效.」
         */
        binding.chipGroup1.setOnCheckedChangeListener { group, _ ->
            group.checkedChipIds.map {
                findViewById<Chip>(it)
            }.map {
                it.text
            }.toString() toast this
        }

        // 霸王硬上弓
        binding.chipGroup1.forEach { childView ->
            if (childView is Chip) {
                // 点击监听
                childView.click {
                    "${childView.text}-${childView.isChecked}" toast this
                }

                // 关闭监听
                childView.setOnCloseIconClickListener {

                }
            }
        }

        binding.printChipGroup.click {
            binding.printChipGroup.text = binding.chipGroup1.checkedChipIds.map {
                findViewById<Chip>(it)
            }.joinToString(",") {
                it.text
            }

            binding.printChipGroup.text toast this
        }


        /*
         * 作者:android 超级兵
         * 创建时间: 1/8/22 3:02 PM
         * TODO 点击监听
         */
        binding.chipGroup2.setOnCheckedChangeListener { _, checkedId ->
            findViewById<Chip>(checkedId)?.text ?: "取消" toast this
        }


        /*
         * 作者:android 超级兵
         * 创建时间: 1/14/22 10:51 AM
         * TODO 动态添加view
         */
        resources.getStringArray(R.array.chips_item2).forEach {
            val chip =
                layoutInflater.inflate(
                    R.layout.item_chip_second, binding.chipGroup2,
                    false
                ) as Chip
            chip.apply {
                text = it
            }
            binding.chipGroup2.addView(chip)
        }






        // 选中图标和选中颜色
        resources.getStringArray(R.array.chips_item1).forEach {
            val chip =
                layoutInflater.inflate(R.layout.item_chip, binding.chipGroup3, false) as Chip

            chip.apply {
                text = it
                // 设置右侧删除按钮
                isCloseIconVisible = true

                // 删除监听
                chip.setOnCloseIconClickListener {
                    if (binding.chipGroup3.contains(it)) {
                        "已删除:${chip.text}" toast this@ChipActivity
                        binding.chipGroup3.removeView(it)
                    }
                }

                // 添加chip
                binding.chipGroup3.addView(this)
            }
        }




        binding.chipGroup3.setOnHierarchyChangeListener(object :
            ViewGroup.OnHierarchyChangeListener {
            override fun onChildViewAdded(parent: View?, child: View) {
                // 添加监听
                Log.i(this@ChipActivity.TAG, "chip add:${child.takeView<Chip>()?.text}")
            }

            override fun onChildViewRemoved(parent: View?, child: View) {
                // 删除监听
                Log.i(this@ChipActivity.TAG, "chip remove:${child.takeView<Chip>()?.text}")
            }
        })
    }
}